package com.xxx.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xxx.domain.Login;
import com.xxx.domain.User;
import com.xxx.domain.UserAccount;
import com.xxx.domain.UserBaseInfo;
import com.xxx.dto.UserRegisterDto;
import com.xxx.feignclients.LoginFeignClient;
import com.xxx.mapper.UserMapper;
import com.xxx.result.JSONResult;
import com.xxx.service.IUserAccountService;
import com.xxx.service.IUserBaseInfoService;
import com.xxx.service.IUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xxx.util.AssertUtil;
import com.xxx.util.BitStatesUtils;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.jws.soap.SOAPBinding;
import javax.swing.text.html.parser.Entity;
import java.math.BigDecimal;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author jh
 * @since 2025-09-01
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private IUserAccountService userAccountService;
    @Autowired
    private IUserBaseInfoService userBaseInfoService;
    @Autowired
    private LoginFeignClient loginFeignClient;

    @Override
    @GlobalTransactional // 开启全局事务
    public void register(UserRegisterDto userRegisterDto) {
        String phone = userRegisterDto.getMobile();
        String password = userRegisterDto.getPassword();
        Integer regChannel = userRegisterDto.getRegChannel();
        String smsCodeFromUser = userRegisterDto.getSmsCode();

        String smsCodeKey = "smsreg:" + phone;
        Object smsCodeValue = redisTemplate.opsForValue().get(smsCodeKey);
        String[] smsCodeValueArr = smsCodeValue.toString().split(":");

        //判断验证码是否一致,并使用全局异常处理
        AssertUtil.isEquals(smsCodeFromUser, smsCodeValueArr[0]);

        Wrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("phone", phone);  //where  phone=(phone)1548889
        User userFromDb = selectOne(wrapper);
        AssertUtil.isNull(userFromDb, "手机号已被注册 ");
        /**
         * 调用feign接口,调用uaa服务保存logininfo 返回id
         * 补充ymcc-uaa-service的login表
         */
        Login login = new Login();
        login.setAccountNonExpired(true);
        login.setAccountNonLocked(true);
        login.setEnabled(true);
        login.setPassword(password);
        login.setCredentialsNonExpired(true);
        login.setType(Login.TYPE_USER);
        login.setUsername(phone);
        login.setClientId("website");
        login.setAvatar("https://iknow-pic.cdn.bcebos.com/8b13632762d0f703c529675f1afa513d2797c59e");
        JSONResult jsonResult = loginFeignClient.saveOrUpdate(login);
        AssertUtil.isTrue(jsonResult.isSuccess(), jsonResult.getMessage());
        Long logininfoId = Long.valueOf(jsonResult.getData().toString());
        /**
         * 补充ymcc-pojo-user的user表
         */
        long currentTimeMillis = System.currentTimeMillis();
        User user = new User();
        user.setPhone(phone);
        user.setCreateTime(currentTimeMillis);
        user.setLoginId(logininfoId);
        Long userState = 0L;
        BitStatesUtils.addState(userState, BitStatesUtils.OP_REGISTED);       // 已注册
        BitStatesUtils.addState(userState, BitStatesUtils.OP_ACTIVED);       // 已激活
        BitStatesUtils.addState(userState, BitStatesUtils.OP_AUTHED_PHONE); // 已认证手机
        user.setBitState(userState);
        insert(user);
        /**
         * 补充ymcc-pojo-user的user_account表
         */
        UserAccount userAccount = new UserAccount();
        userAccount.setCreateTime(currentTimeMillis);
        userAccount.setFrozenAmount(new BigDecimal(0));
        userAccount.setUsableAmount(new BigDecimal(0));
        userAccount.setId(user.getId());
        userAccountService.insert(userAccount);
        /**
         * 补充ymcc-pojo-user的t_user_base_info表
         */
        UserBaseInfo userBaseInfo = new UserBaseInfo();
        userBaseInfo.setCreateTime(currentTimeMillis);
        userBaseInfo.setRegChannel(regChannel);
        userBaseInfo.setId(user.getId());
        userBaseInfoService.insert(userBaseInfo);
        //删除redis中的验证码
        redisTemplate.delete(smsCodeKey);
    }
}
